package 晚间作业0321;

import java.util.*;

public class Doc13 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));
        q1(list, "3");
        System.out.println(list);
        System.out.println("=========================================");

        List<Double> list1 = new ArrayList<>(Arrays.asList(2.0, 3.0, 4.0, 5.0));
        q2(list1, 3);
        System.out.println(list1);
        System.out.println("=========================================");

        MyArrayList<String> myArrayList = new MyArrayList<>(0);
        myArrayList.add("1");
        myArrayList.add("4");
        myArrayList.add("3");
        myArrayList.add("2");
        myArrayList.add("10");
        myArrayList.add("10");
        myArrayList.add("9");
        myArrayList.add("5");
        System.out.println(myArrayList);
        myArrayList.remove(0);
        System.out.println(myArrayList);
        myArrayList.remove("10");
        System.out.println(myArrayList);
        myArrayList.set(3, "8");
        System.out.println(myArrayList);
        myArrayList.sort(String::compareTo);
        System.out.println(myArrayList);
        myArrayList.clear();
        System.out.println(myArrayList);
    }

    /**
     * 1. 【简】设计一个方法，删除一个集合中，所有的指定的元素。例如，将集合中所有的3都删除。
     */
    public static void q1(List<String> list, String delItem) {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (delItem.equals(next)) {
                iterator.remove();
            }
        }
    }

    /**
     * 2. 【简】设计一个方法，将一个存储有体重信息的集合，所有的数据乘2
     */
    public static void q2(List<Double> weightList, int multiple) {
        for (int i = 0; i < weightList.size(); i++) {
            weightList.set(i, weightList.get(i) * multiple);
        }
    }

    /**
     * 3. 【简】设计一个方法，在一个存储了若干个视频的集合中，删除所有的以 .mp4 结尾的元素。
     */
    public static void q3(List<String> files, String suffix) {
        Iterator<String> iterator = files.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.endsWith(suffix)) {
                iterator.remove();
            }
        }
    }

    /**
     * 附加题：【难】设计一个MyArrayList类，实现不定长度实现的基本功能:
     * 1. MyArrayList中添加的元素，可以通过泛型进行限定
     * 2. 向MyArrayList中添加元素
     * 3. 从MyArrayList中删除元素（按照下标删除、按照元素删除、清空所有）
     * 4. 通过下标，获取元素
     * 5. 通过下标，修改元素
     * 6. 将MyArrayList中的元素进行升序排序
     */
    public static class MyArrayList<T> {
        public static final int INIT_LEN = 16;
        private Object[] items;
        private int curCapacity;
        private int size;

        public MyArrayList() {
            this(INIT_LEN);
        }

        public MyArrayList(int len) {
            items = new Object[len];
            curCapacity = len;
            size = 0;
        }

        private void ensureCapacity() {
            if (((curCapacity * 3) >> 2) <= size + 1) {
                curCapacity = (curCapacity * 3 >> 1) + 1;
                items = Arrays.copyOf(items, curCapacity);
            }
        }

        private void rangeCheck(int pos) {
            if (pos < 0) {
                throw new IllegalArgumentException("非法位置");
            } else if (pos > size){
                throw new IndexOutOfBoundsException("传入pos=" + pos + ", 当前数组长len=" + size);
            }
        }

        public void add(T item) {
            ensureCapacity();
            items[size] = item;
            size++;
        }

        public void remove(int pos) {
            rangeCheck(pos);
            System.arraycopy(items, pos + 1, items, pos, curCapacity - pos - 1);
            items[--size] = null;
        }

        public void remove(T item) {
            int i = size - 1;
            while (i >= 0) {
                if (items[i].equals(item)) {
                    remove(i);
                }
                i--;
            }
        }

        public void clear() {
            for (int i = 0; i < size; i++) {
                items[i] = null;
            }
            items = new Object[INIT_LEN];
            curCapacity = INIT_LEN;
            size = 0;
        }

        @SuppressWarnings("unchecked")
        public T get(int pos) {
            rangeCheck(pos);
            return (T) items[pos];
        }

        public void set(int pos, T item) {
            rangeCheck(pos);
            items[pos] = item;
        }

        @SuppressWarnings("unchecked")
        public void sort(Comparator<T> comparator) {
            if (size <= 1) { return; }
            boolean flag = false;
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size - i - 1; j++) {
                    if (comparator.compare((T) items[j + 1], (T) items[j]) < 0) {
                        Object temp = items[j];
                        items[j] = items[j+1];
                        items[j+1] = temp;
                        flag = true;
                    }
                }
                if (!flag) {
                    break;
                }
            }
        }

        @Override
        public String toString() {
            return Arrays.toString(Arrays.copyOf(items, size));
        }
    }

}


